home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 403 < prev    next >
Internet Message Format  |  1996-08-06  |  2KB

  1. Path: solon.com!not-for-mail
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.std.c,comp.lang.c.moderated
  4. Subject: Re: Integral promotion.
  5. Date: 15 Feb 1996 09:42:04 -0600
  6. Organization: Carelcomp Forest
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4fvk8c$eq8@solutions.solon.com>
  10. References: <4fstj7$2l6@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  13.  
  14. Rune Huseby wrote:
  15. : I got a problem understanding the rules on integral promotion in
  16. : the C language:
  17. : Consider the following function:
  18. : short test(short x1, short x2);
  19. : int main(void)
  20. : {
  21. :   short result;
  22. :   result = test(1, 2);
  23.  
  24. The numbers 1 and 2 here are by convention considered by compiler to be ints (not 
  25. shorts).
  26.  
  27. :   return 0;
  28. : }
  29. : short test(short x1, short x2)
  30. : {
  31. :   short result;
  32. :   result = x1 + x2;  /* Warning:  '=' : conversion from 'int '
  33. :                          to 'short ', possible loss of data */
  34. :   return result;
  35. : }
  36. : My compiler (Microsoft Visual C++ 4.0), automagically converts
  37. : my short-parameters to int's, even though all variables involved
  38. : are short. I know that the standard says that all arguments can
  39. : be converted to the biggest 'type' of all the arguments, but is
  40. : it correct that char's and short's always are promoted to int's,
  41. : without regard to the other arguments in the expression?
  42.  
  43. Chars are converted to ints, because passing a byte (where char equals one byte in 
  44. size) is both inefficient and leads to difficulties in the receiving party (for 
  45. instance, to pass two chars would then pack them into one byte, which the receiver 
  46. would have to be able to handle).
  47.  
  48. : The reason I ask is that I am writing code that should be
  49. : portable from a 16-bits environment (where short and int are the
  50. : same size) to a 32-bits environment. (My 16-bits compiler does
  51. : not complain about the code).
  52.  
  53. That's because in 16-bits, sizeof(int) == sizeof(short) in PC world.
  54.  
  55. Later,
  56.  AriL
  57. -- 
  58. All my opinions are mine and mine alone.
  59.